package customview.ike.com.customviewproject;

import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.util.jar.Manifest;

import customview.ike.com.customviewproject.R;
import customview.ike.com.customviewproject.utils.permission.PermissionHelper;
import customview.ike.com.customviewproject.utils.permission.PermissionSuccess;

/**
 * Created by Administrator on 2017/12/8.
 */

public class PermissionActivity extends AppCompatActivity {
    private String Tag="PermissionActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
    }
    public void  openCamera(View view){
//        PermissionHelper.with(this).addRequestCode(101)
//                .addRequestPermissions(android.Manifest.permission.CAMERA)
//                .request();
        
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionHelper.getInstance().handleRequestPermissionsResult(requestCode,permissions,grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
    @PermissionSuccess(requestCode = 101)
    private void  openCamera(){
        Log.e(Tag,"开启相机");
        checkHasCameraPermession(this,101);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivityForResult(intent,3);
        
    }
    private void checkHasCameraPermession(Context context, int requestCode){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            int checkResult = appOpsManager.checkOpNoThrow(
                    AppOpsManager.OPSTR_CAMERA, Binder.getCallingUid(), context.getPackageName());
            if(checkResult == AppOpsManager.MODE_ALLOWED){
                Log.e("jijiaxin","有权限");
            }else if(checkResult == AppOpsManager.MODE_IGNORED){
                Log.e(Tag,"1");
                Log.e("jijiaxin","被禁止了");

            }else if(checkResult == AppOpsManager.MODE_ERRORED){
                Log.e(Tag,"2");
                Log.e("jijiaxin","出错了");
            }else if(checkResult == 4){
                Log.e("jijiaxin","权限需要询问");
            }
        }

    }
}
